package cn.jhz.learn.blog.config.datasource;

import java.io.IOException;
import java.util.Properties;

import javax.sql.DataSource;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.jetbrains.annotations.NotNull;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.lang.NonNull;
import org.springframework.lang.NonNullApi;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;



@Configuration
@MapperScan(
		basePackages={
				"cn.jhz.learn.blog.dao.article",
				"cn.jhz.learn.blog.dao.blogger",
//				"cn.jhz.learn.blog.dao.essay",
//				"cn.jhz.learn.blog.dao.guestbook",
				"cn.jhz.learn.blog.dao.file",
				"cn.jhz.learn.blog.dao.other",
				"cn.jhz.learn.blog.dao.photo"
		}, sqlSessionFactoryRef = "blogSqlSessionFactory")
public class BlogDataConfig implements TransactionManagementConfigurer{

	@Primary
	@Bean("blogDataSource")
	public DataSource dataSource() {
		try {
			Resource resource = new ClassPathResource("db.properties");
			Properties props = PropertiesLoaderUtils.loadProperties(resource);
			DruidDataSource ds = new DruidDataSource();


			ds.setDriverClassName(props.getProperty("jdbc.driver"));
			ds.setUrl(props.getProperty("jdbc.blog.url"));
			ds.setUsername(props.getProperty("jdbc.username"));
			ds.setPassword(props.getProperty("jdbc.password"));
			ds.setInitialSize(3);
			ds.setMinIdle(3);
			ds.setMaxActive(20);
			ds.setMaxWait(60000);
			//ds.setFilters("stat,wall");
//			List<Filter> filterList = new ArrayList<>();
//			filterList.add(wallFilter(wallConfig()));
//			filterList.add(statFilter());
//			ds.setProxyFilters(filterList);
			
			return ds;
		}catch (IOException ioE) {
			ioE.printStackTrace();
		}
		return null;
	}

	@Primary
	@Bean("pageHelper")
	public Interceptor pageHelper(){
		Interceptor interceptor = new PageInterceptor();
		Properties properties = new Properties();

		properties.setProperty("helperDialect", "mysql");
		properties.setProperty("offsetAsPageNum", "true");
		properties.setProperty("reasonable", "true");
		properties.setProperty("rowBoundsWithCount","true");

		interceptor.setProperties(properties);

		return interceptor;
	}

	@Primary
	@Bean("blogSqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory(@Qualifier("blogDataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sf = new SqlSessionFactoryBean();

		sf.setPlugins(pageHelper());
		sf.setDataSource(dataSource);
		sf.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/blog/*/*.xml"));

		return sf.getObject();
	}

	@Primary
	@Bean("blogSqlSessionTemplate")
	public SqlSessionTemplate sqlSessionTemplate(@Qualifier("blogSqlSessionFactory")SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
	}

	@Override
	@Primary
	@Bean(name = "blogTransactionManager")
	public PlatformTransactionManager annotationDrivenTransactionManager() {
	       DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
	        //设置事务管理器管理的数据源
	        transactionManager.setDataSource(dataSource());
	        return transactionManager;
	}


//	@Bean
//	public StatFilter statFilter() {
//		StatFilter statFilter = new StatFilter();
//		statFilter.setSlowSqlMillis(300);
//		statFilter.setLogSlowSql(true);
//		statFilter.setMergeSql(true);
//
//		return statFilter;
//	}
//
//	@Bean
//	public WallConfig wallConfig(){
//        WallConfig wallConfig = new WallConfig();
//        wallConfig.setMultiStatementAllow(true);//允许一次执行多条语句
//        wallConfig.setNoneBaseStatementAllow(true);//允许一次执行多条语句
//        return wallConfig;
//    }
//
//	@Bean
//	@DependsOn
//	public WallFilter wallFilter(WallConfig wallConfig) {
//		WallFilter wallFilter = new WallFilter();
//		wallFilter.setConfig(wallConfig);
//		wallFilter.setDbType("mysql");
//
//		return wallFilter;
//	}
}
